package com.example.demo.controller;

import com.example.demo.entity.About;
import com.example.demo.service.IAboutService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AboutController {

	@Autowired
	private IAboutService aboutService;
	
	@RequestMapping("/aboutList")
	public ModelAndView list(@RequestParam(value="pageNum",defaultValue="1")Integer pageNum) {
		PageHelper.startPage(pageNum, 3);
		List<About> aboutList = aboutService.selectAll();
		PageInfo pageInfo=new PageInfo(aboutService.selectAll());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("aboutList",aboutList);
		modelAndView.addObject("pageInfo",pageInfo);
		modelAndView.setViewName("admin/about/aboutList");
		return modelAndView;
	}
	
	@RequestMapping("/toEditAbout")
	public String toedit(Model model, Integer id) {
		if (id != null) {
			About about = aboutService.selectById(id);

			model.addAttribute("about",about);
		}
		return "admin/about/edit";
	}
	
	@RequestMapping("/editAbout")
	public String edit(About about) {

		if (about.getId() != null ) {
			aboutService.update(about);
		} else {
			aboutService.insert(about);
		}

		return "redirect:aboutList";
	}
	
	@RequestMapping("/deleteAbout")
	public String delete(Integer id) {

		aboutService.delete(id);

		return "redirect:aboutList";
	}
	
}
